package hanlder

import (
	"encoding/json"
	"order-go/cache"
	"order-go/models"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var order models.OrderEsList
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("order_es").
			Select(
				"order_es.id AS OrderId",
				"order_es.es_info AS EsInfo",
				"order_es.es_money AS EsMoney",
				"order_es.es_num AS EsNum",
				"orders.order_name AS OrderName",
				"orders.order_price AS OrderPrice",
				"orders.order_status AS OrderStatus",
			).Joins("LEFT JOIN orders ON order_es.id=orders.id").
			Where("order_es.id=?", id).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	return
}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	//	SELECT
	//order_es.id AS OrderId,
	//order_es.es_info AS EsInfo,
	//order_es.es_money AS EsMoney,
	//order_es.es_num AS EsNum,
	//orders.order_name AS OrderName,
	//orders.order_price AS OrderPrice,
	//orders.order_status AS OrderStatus
	//FROM
	//order_es
	//LEFT JOIN orders ON order_es.id=orders.id
	var order []models.OrderEsList
	redis_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("order_es").
			Select(
				"order_es.id AS OrderId",
				"order_es.es_info AS EsInfo",
				"order_es.es_money AS EsMoney",
				"order_es.es_num AS EsNum",
				"orders.order_name AS OrderName",
				"orders.order_price AS OrderPrice",
				"orders.order_status AS OrderStatus",
			).Joins("LEFT JOIN orders ON order_es.id=orders.id").
			Offset(offset).Limit(size).Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	var total int64
	models.DB.Model(&models.OrderEs{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  order,
		"total": total,
	})
	return
}
func OrderEsAdd(c *gin.Context) {
	es_num := c.PostForm("es_num")
	es_money := c.PostForm("es_money")
	es_info := c.PostForm("es_info")
	data := models.OrderEs{
		Model:   gorm.Model{},
		EsNum:   es_num,
		EsMoney: es_money,
		EsInfo:  es_info,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderAdd(c *gin.Context) {
	order_name := c.PostForm("order_name")
	order_price := c.PostForm("order_price")
	order_status := c.PostForm("order_status")
	data := models.Order{
		Model:       gorm.Model{},
		OrderName:   order_name,
		OrderPrice:  order_price,
		OrderStatus: order_status,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
